<?php
/**
 * DolphinGTK - phpGtk framework
 *
 * Графический модуль
 *
 * @package    DolphinGTK
 * @author     Askarov Ruslan
 * @copyright  Askarov Ruslan (c) 2010.
 * @link       http://groups.google.com/group/dolphingtk
 * @license    http://www.opensource.org/licenses/gpl-license.php GPL
 * @version    0.1
 * @since      PHP v.: 5.2.1
 * @subpackage desktop
 * @category   module
 * @filesource
 */


class Pppoe
{
  private static $pppoe_proc = 'ps -e | grep pppd | awk \'{print $1}\' | tail -1';
  private static $status = 0;
  private static $pulse;
  private static $auto_connect = true;


  public function set()
  {
    $apache = Widgets::button(
      'Connect', 'connect', array('Pppoe','connect'), array(145,-1)
    );
    $mysql  = Widgets::button(
      'Disconnect', 'disconnect', array('Pppoe','disconnect'), array(145,-1)
    );

    self::$pulse = new GtkProgressBar();
    self::$pulse->set_pulse_step(0.05);

    $control = new GtkVBox();
    $hbox    = new GtkHBox();
    $control->pack_start(self::$pulse, 0, 0);

    $control->pack_start($hbox, 0, 0);
    $hbox->pack_start($apache, 0, 0);
    $hbox->pack_start($mysql, 0, 0);

    self::disconnect();

    Win::timeout('add', array('Pppoe','pulse_hide'), 1);
    Win::timeout('add', array('Pppoe','check_connection'), 1000);
    
    return $control;
  }




  public function pulse()
  {
    self::$pulse->pulse();
    Win::timeout('add', array('Pppoe','pulse'), 200);
  }




  public function pulse_hide()
  {
    self::$pulse->hide();
  }




  public function connect()
  {
    if( Win::$os == 'linux' ):
    Win::timeout('remove', array('Pppoe','check_connection'), 1);

    if( self::$status === 0 ) {
      self::$pulse->hide();
      self::$status = 1;
      Win::timeout('add', array('Pppoe','check_connection'), 1);
    }
    
    $proc_id = OS::exec(self::$pppoe_proc);

    if( !is_numeric($proc_id) ) {
      self::$pulse->show();
      OS::exec('pon dsl-provider >& /dev/null &');
      Win::timeout('add', array('Pppoe','pulse',1), 200);
    }
    endif;
  }




  public function disconnect()
  {
    if( Win::$os == 'linux' ):
    //OS::exec('poff dsl-provider >& /dev/null &');

    Win::timeout('remove', array('Pppoe','pulse'), 1);
    Win::timeout('remove', array('Pppoe','check_connection'), 10);
    Win::timeout('add', array('Pppoe','pulse_hide'), 20);

    if( self::$status === 1 or self::$status === 2 ) {

      Tray::icon('images/pppoe-disconnected.png');
      Win::statusbar_update( i18n('Disconnected') );
      Tray::tooltips( 'DolphinGTK / PPPoE / ' . i18n('Disconnected') );

      Tray::notify(
        array(
          'DolphinGTK PPPoE',
          "PPPoE daemon is shutdown."
        )
      );
    }

    self::$status = 0;
    self::$pulse->hide();

    $proc_id = OS::exec(self::$pppoe_proc);

    if( is_numeric($proc_id) ) {
      OS::exec("kill -9 $proc_id >& /dev/null &");
    }
    endif;
  }




  public function check_connection()
  {
    if( Win::$os == 'linux' ):
    $proc_id = OS::exec(self::$pppoe_proc);
    $if_ip   = OS::exec('ifconfig | grep -A 1 ppp0 | grep inet | cut -d ":" -f2 | cut -d " " -f1');

    if( !$if_ip )
    {
      Tray::icon('images/pppoe-disconnected.png');
      Win::statusbar_update( i18n('Disconnected') );
      Tray::tooltips( 'DolphinGTK / PPPoE / ' . i18n('Disconnected') );

      if( self::$auto_connect === true ):
      if( !is_numeric($proc_id) ) {
        Tray::notify(
          array(
            'DolphinGTK PPPoE',
            "PPPoE daemon is die. Reanimate daemon."
          )
        );
        self::$status = 1;
        self::connect();
      } else {
        Tray::icon('images/pppoe-connection.png');
        Tray::tooltips('DolphinGTK / PPPoE / ' . i18n('Connection ...') );
        Win::statusbar_update( i18n('Connection ...') );
      }
      endif;

      Win::timeout('add', array('Pppoe','check_connection'), 5000);
    }
    else {
      Tray::icon('images/pppoe-connected.png');
      Win::statusbar_update( i18n('Connected / ') . $if_ip );
      Tray::tooltips('DolphinGTK / PPPoE / ' . i18n('Connected / ') . $if_ip);

      if( self::$status === 1 ) {
        self::$status = 2;
        Win::timeout('remove', array('Pppoe','pulse'), 1);
        Win::timeout('add', array('Pppoe','pulse_hide'), 2);

        Tray::notify(
          array(
            "DolphinGTK PPPoE",
            "PPPoE connection established.\nIP: " . $if_ip
          )
        );

      }
      Win::timeout('add', array('Pppoe','check_connection'), 60000);
    }

    endif;
  }

}
?>
